<?php
namespace api\modules\v1\controllers\patient;


use common\models\base\Project;
use common\models\patient\User;
use Yii;
use yii\web\Controller;

class DengtaController extends Controller
{
    public $enableCsrfValidation = false;

    /**
     * @apiName 根据手机号 / 微信号 匹配
     * @apiGroup
     * @apiParam {Number}
     * @apiParam {String}
     * @param $mobile 患者手机号
     * @param $wechatId 患者微信号
     * @return bool
     * @author loujie
     */
    public static function actionFindBaiyou($name_sx, $mobile = null, $wechatId = null)
    {
        if (empty($mobile) && empty($wechatId)) {
            return false;
        }

        $mobileArray = [];
        if (!empty($mobile)) {
            $mobileArray = array_filter(explode(',',$mobile));
        }

        $query = User::find()
            ->alias('u')
            ->innerJoin(Project::tableName() . 'p','u.project_id = p.id')
            ->andwhere(['p.name_sx' => $name_sx]);
        $query->andWhere([
            'or',
            !empty($mobileArray) ? ['u.mobile' => $mobileArray] : [],
            !empty($wechatId) ? ['u.wx_name' => $wechatId] : [],
        ]);

        return $query->exists();
    }

    /**
     * @api {post} /v1/patient/dengta/find-all-baiyou 编辑患者
     * @apiName update
     * @apiGroup Index
     *
     * @apiParam {Number} name_sx                   项目缩写名
     * @apiParam {Array}  mobiles                   手机号
     * @apiParam {Array}  wechat_ids                微信号
     *
     * @apiSuccess {Object}  data                   返回数据
     * @apiSuccess {Number}  data.list              患者列表
     */
    public function actionFindAllBaiyou()
    {
        $params = Yii::$app->request->post();
        $name_sx = $params['name_sx'] ?? [];
        $mobile = array_filter($params['mobiles'] ?? []);
        $wechatId = array_filter($params['wechat_ids'] ?? []);
        if (empty($mobile) && empty($wechatId)) {
            return [];
        }
        /*$mobileArray = [];
        if (!empty($mobile)) {
            $mobileArray = array_filter(explode(',',$mobile));
        }*/

        $query = User::find()->alias('u')
            ->select(['u.id', 'u.mobile', 'u.wx_name'])
            ->innerJoin(Project::tableName() . 'p','u.project_id = p.id')
            ->andwhere(['p.name_sx' => $name_sx]);
        $query->andWhere([
            'or',
            !empty($mobile) ? ['u.mobile' => $mobile] : [],
            !empty($wechatId) ? ['u.wx_name' => $wechatId] : [],
        ]);
        return $query->asArray()->all();
    }
}